#include"lenet5.h"
#include<iostream>
#define NUM 100
using namespace std;

float images[NUM][1][28][28];
int label[NUM];

data_t data[NUM][1][28][28];
data_t out[10];

data_t Wf1[120][400]={
#include"E:\\hls_project\\lenet5\\weight\\Wf1.h"
};
data_t bf1[120]={
#include"E:\\hls_project\\lenet5\\weight\\bf1.h"
};
data_t Wf2[84][120]={
#include"E:\\hls_project\\lenet5\\weight\\Wf2.h"
};
data_t bf2[84]={
#include"E:\\hls_project\\lenet5\\weight\\bf2.h"
};
data_t Wf3[10][84]={
#include"E:\\hls_project\\lenet5\\weight\\Wf3.h"
};
data_t bf3[10]={
#include"E:\\hls_project\\lenet5\\weight\\bf3.h"
};
data_t fc_weight[120*400+84*120+10*84];
data_t fc_bias[120+84+10];

//void fc_baseline(data_t *in,data_t *weight,data_t* bias,data_t* out,int n,int m){
//	for(int i=0;i<m;i++)
//	{
//		data_t tmp=0;
//		for(int j=0;j<n;j++)
//			tmp+=in[j]*weight[i*n+j];
//		*(out+i)=tmp>((data_t)0)?tmp:(data_t)0;
//	}
//}

int main(){
	FILE *fp = fopen("E:\\hls_project\\lenet5\\weight\\test_images.bin","rb");
	fread(images,sizeof(float),NUM*784,fp);
	fclose(fp);
	fp = fopen("E:\\hls_project\\lenet5\\weight\\test_labels.bin","rb");
	fread(label,sizeof(int),NUM,fp);
	fclose(fp);
	for(int i=0;i<NUM*784;i++){
		*((data_t*)data+i)=(data_t)(*((float*)images+i));
	}
	//fc_weight
	for(int i=0;i<(120*400+84*120+10*84);i++){
		if(i>=0&&i<120*400)
			fc_weight[i]=*((data_t*)Wf1+i);
		else if(i>=120*400&&i<(120*400+84*120))
			fc_weight[i]=*((data_t*)Wf2+(i-120*400));
		else
			fc_weight[i]=*((data_t*)Wf3+(i-120*400-84*120));
	}
	//fc_bias
	for(int i=0;i<(120+84+10);i++){
		if(i<120){
			fc_bias[i]=bf1[i];
		}
		else if(i>=120&&i<(120+84)){
			fc_bias[i]=bf2[i-120];
		}
		else{
			fc_bias[i]=bf3[i-120-84];
		}
	}
//	for(int i=0;i<100*784;i++)
//		cout<<*((data_t*)data+i)<<endl;
	//
	int correct=0;
	for(int i=0;i<NUM;i++){
		LeNet5((data_t*)data[i],(data_t*)out,(packed_data_t*)fc_weight,(data_t*)fc_bias);
		data_t max=-999;
		int max_idx=-1;
		for(int k=0;k<10;k++)
			if(out[k]>max){
				max=out[k];
				max_idx=k;
			}
		if(max_idx==label[i]){
			cout<<i<<",right"<<endl;
			correct++;
		}
	}
	cout<<"accuracy is "<<correct/(float)NUM<<endl;
	return 0;
}
